//
//  FanpageViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/18.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//**************粉丝**************
#import "FanpageViewController.h"

@interface FanpageViewController ()<UITableViewDataSource,UITableViewDelegate>
/*
 *数据源
 */
@property(nonatomic,copy)NSMutableArray *dataArray;

@property(nonatomic,copy)UITableView *myTable;
@end

@implementation FanpageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createNavigation];
    [self createUI];
    [self getMyHttpData];
    // Do any additional setup after loading the view.
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"粉丝";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 34, self.view.frame.size.width, self.view.frame.size.height-34) style:UITableViewStyleGrouped];
    table.showsVerticalScrollIndicator=NO;
    table.delegate=self;
    table.dataSource=self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    _myTable=table;

}
//tableView的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //用户头像
    UIImageView *userHeadImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 56, 56)];
    //userHeadImage.image=[UIImage imageNamed:_dataArray[indexPath.row][@"userHeadImage"]];
    [userHeadImage setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.row][@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1.png"]];
    userHeadImage.layer.masksToBounds=YES;
    userHeadImage.layer.cornerRadius=userHeadImage.frame.size.width/2.0;
    [cell.contentView addSubview:userHeadImage];
    
    //用户名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userHeadImage.frame)+8, userHeadImage.frame.origin.y+10, 10, 36)];
    name.text=_dataArray[indexPath.row][@"nikename"];
    name.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    name.font=[UIFont systemFontOfSize:14];
    name.frame=CGRectMake(name.frame.origin.x, name.frame.origin.y, [self calculateWidthWithLable:name], name.frame.size.height);
    [cell.contentView addSubview:name];
    
    //性别
    UIImageView *sex=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(name.frame)+2, name.frame.origin.y+(36-20)/2.0, 20, 20)];
    [cell.contentView addSubview:sex];
    NSInteger sexState=[_dataArray[indexPath.row][@"sex"] integerValue];
    if (sexState==0) {
        sex.image=[UIImage imageNamed:@"sex_man"];
    }else{
        sex.image=[UIImage imageNamed:@"sex_woman"];
    }

    
    
    
    
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 75, self.view.frame.size.width, 1)];
    line.backgroundColor=[UIColor grayColor];
    [cell.contentView addSubview:line];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 76;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSInteger currentId=[[user objectForKey:@"userId"]integerValue];
    NSInteger httpUserId=[_dataArray[indexPath.row][@"user_id"] integerValue];
    if (currentId == httpUserId) {
        //当前用户
        SelfViewController *vc=[[SelfViewController alloc]init];
        self.tabBarController.tabBar.hidden=YES;
        vc.userId=httpUserId;
        vc.barState=1;
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        MyAttentionViewController *vc=[[MyAttentionViewController alloc]init];
        vc.barState=1;
        vc.userId=_dataArray[indexPath.row][@"user_id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
   
}

//根据文字计算宽度和高度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)getMyHttpData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];

    
    [manager GET:[NSString stringWithFormat:MyFans,_userId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (arr) {
            _dataArray = [NSMutableArray arrayWithArray:arr];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_myTable reloadData];
            });
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden=NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
